UIHeroDetails = UIBase.New(PanelNames.UIHeroDetails)

function UIHeroDetails:Init(heroId)
    self.heroId = heroId
end

function UIHeroDetails:OnCreate()
    self.txtHeroName = self:GetChild("Text_HeroName")
    self.ctrlHeroType = self:GetController("c1")
    self.comHeroInfo = self:GetChild("Com_CardTypeNew")
    self.listStar = self.comHeroInfo:GetChild("List_Star")
    self.txtType1 = self.comHeroInfo:GetChild("Text_Type01")
    self.txtType2 = self.comHeroInfo:GetChild("Text_Type02")
    self.imgPhoto  = self:GetChild("Loader_Photo")
    self.comSkill = self:GetChild("Com_DetailsSkill")
    self.imgSkill = self.comSkill:GetChild("Loader_Icon")
    self.txtSkillDesc = self.comSkill:GetChild("Text_SkillDesc")
    self.txtCharactorDesc = self:GetChild("Text_CharacteristicDes")

    self.comAttribute = self:GetChild("Com_CardAttributeNew")
    self.txtAttack = self.comAttribute:GetChild("text_attack")
    self.txtDef = self.comAttribute:GetChild("text_def")
    self.txtLife = self.comAttribute:GetChild("text_life")
    self.txtMove = self.comAttribute:GetChild("text_move")
    self.txtSpeed = self.comAttribute:GetChild("text_speed")
    self.txtRange = self.comAttribute:GetChild("text_range")

    self.btnBack = self:GetChild("Btn_Back")
    self:AddEvent()
    self:InitView()
end

function UIHeroDetails:AddEvent()
    self:RegisterOnClick(self.btnBack,self.OnClickBack)
    self:SetItemRenderer(self.listStar,self.StarItemRender, nil, true)
end

function UIHeroDetails:InitView()
    self.heroCfg = DataTable.GetById("t_hero",self.heroId)
    self.heroResourceCfg = DataTable.GetById("t_heroresources",self.heroId)
    if not (self.heroCfg and self.heroResourceCfg) then return end

    local heroSkillID = self.heroCfg.f_SkillID
    local skillShowCfg = DataTable.GetById("t_skillshow",heroSkillID)
    local skillCfg = DataTable.GetById("t_skill",heroSkillID)
    self.imgSkill.url = skillShowCfg.f_SkillIcon
    self.txtSkillDesc.text = skillCfg.f_SkillDescribeKey

    self.consume = self.heroCfg.f_ConsumeEnergy
    self.listStar.numItems = 10

    self.txtCharactorDesc.text = ""
    if self.heroCfg.f_CompanyType == Battle.ActorType.Hero then
        self.ctrlHeroType.selectedIndex = 0
    elseif  self.heroCfg.f_CompanyType == Battle.ActorType.Lord then
        self.ctrlHeroType.selectedIndex = 1
        local charactorSkillId = self.heroCfg.f_Characteristic
        local charactorSkillCfg = DataTable.GetById("t_skill",charactorSkillId)
        self.txtCharactorDesc.text = charactorSkillCfg.f_SkillDescribeKey
    end

    self.txtHeroName.text = self.heroCfg.f_HeroName

    self.imgPhoto.url = self.heroResourceCfg.f_DetailsChart

    local heroAttribute = decodeJsonStrToTable(self.heroCfg.f_HeroAttribute)
    self.txtAttack.text = heroAttribute[1][2]
    self.txtDef.text = heroAttribute[2][2]
    self.txtLife.text = heroAttribute[3][2]
    self.txtMove.text = heroAttribute[4][2]
    self.txtSpeed.text = heroAttribute[5][2]
    self.txtRange.text = heroAttribute[6][2]

    local heroType = self.heroCfg.f_HeroType
    self.txtType2.text = ""
    if heroType == 1 then
        self.txtType1.text = "普通"
    elseif heroType == 2 then
        self.txtType1.text = "飞行"
    end
end

function UIHeroDetails:StarItemRender(index, go)
    local ctrlFull = go:GetController("Type")
    if index <= self.consume then
        ctrlFull.selectedIndex = 0
    else
        ctrlFull.selectedIndex = 1
    end
end

function UIHeroDetails:OnClickBack()
    closeUI(PanelNames.UIHeroDetails)
end

return UIHeroDetails